package cn.example.common.protocol.modules;

import org.artifact.core.lang.IServer;
import org.artifact.core.context.session.ISession;
import org.artifact.core.context.packet.IPacket;
import cn.hutool.core.map.MapProxy;
import java.util.function.Consumer;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import org.artifact.core.constant.ArtifactErrorCode;
import org.artifact.core.exception.ArtifactExceptionUtil;
import org.artifact.core.lang.IModule;
import cn.example.common.protocol.structs.*;

@SuppressWarnings("unchecked")
public interface MailModule extends IModule{
	
	default int getModuleId(){
		return 1463;
	}
	
	/** 分发  */
	default Consumer<IPacket> disp(ISession session,IPacket packet) {
		switch (packet.getMethodId()) {
			case 22934:{
				return read(session,packet);
			}
			case 1451:{
				return delete(session,packet);
			}
			case 29411:{
				return receive(session,packet);
			}
			default:
				throw ArtifactExceptionUtil.error(ArtifactErrorCode.NOT_FOND_ACTION);
		}
	}	


	/** 读取邮件 */
	default Consumer<IPacket> read(ISession session,IPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		long id = proxy.getLong(3355);
		return read(session, id);
	}
	
	/** 读取邮件 */
	Consumer<IPacket> read(ISession session, long id);
	
	default Consumer<IPacket> _read(ISession session){
		return (IPacket packet) -> {
                if (packet == null){
                    packet = IServer.me().getContext().getPacketFactory().create();
                    packet.setCmd(1463 << 16 ^ 22934);
                } else {
                    packet.getBody().clear();
                }

				session.sendPacket(packet);
			};
	}
	/** 删除邮件 */
	default Consumer<IPacket> delete(ISession session,IPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		long id = proxy.getLong(3355);
		return delete(session, id);
	}
	
	/** 删除邮件 */
	Consumer<IPacket> delete(ISession session, long id);
	
	default Consumer<IPacket> _delete(ISession session){
		return (IPacket packet) -> {
                if (packet == null){
                    packet = IServer.me().getContext().getPacketFactory().create();
                    packet.setCmd(1463 << 16 ^ 1451);
                } else {
                    packet.getBody().clear();
                }

				session.sendPacket(packet);
			};
	}
	/** 收取邮件 */
	default Consumer<IPacket> receive(ISession session,IPacket packet) {
		MapProxy proxy = MapProxy.create(packet.getBody());
		long id = proxy.getLong(3355);
		return receive(session, id);
	}
	
	/** 收取邮件 */
	Consumer<IPacket> receive(ISession session, long id);
	
	default Consumer<IPacket> _receive(ISession session){
		return (IPacket packet) -> {
                if (packet == null){
                    packet = IServer.me().getContext().getPacketFactory().create();
                    packet.setCmd(1463 << 16 ^ 29411);
                } else {
                    packet.getBody().clear();
                }

				session.sendPacket(packet);
			};
	}
	
	
	/** 推送邮件 */
	default void update(ISession session, List<MailDTO> mails){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(1463 << 16 ^ 14537);
		packet.getBody().put(103657884,mails);
		session.sendPacket(packet);
	}
	
	default void update(Collection<ISession> sessions, List<MailDTO> mails){
		IPacket packet = IServer.me().getContext().getPacketFactory().create();
		packet.setCmd(1463 << 16 ^ 14537);
		packet.getBody().put(103657884,mails);
		for (ISession session : sessions) {
			session.sendPacket(packet);
		}
	}
	
}